/***************************************************************************
 *   Copyleft 2008-2009 by												   *
 *	 Iwanicki Krzysztof													   *
 *   <gamma.mini.pw.edu.pl/~iwanickik>									   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 ***************************************************************************/
#include "./vColor.h"

namespace Vendetta {
	const vColor vColor::Red = vColor(1.0f, 0.0f, 0.0f);
	const vColor vColor::Green = vColor(0.0f, 1.0f, 0.0f);
	const vColor vColor::Blue = vColor(0.0f, 0.0f, 1.0f);
	const vColor vColor::White = vColor(1.0f, 1.0f, 1.0f);
	const vColor vColor::Black = vColor(0.0f, 0.0f, 0.0f);

	vColor::vColor(const vColor& color) {
		R = color.R;
		G = color.G;
		B = color.B;
		A = color.A;
	}

	/**************************************************************
	* Operators                                                   *
	***************************************************************/
	vColor& vColor::operator+=(const vColor& color) {
		R += color.R;
		G += color.G;
		B += color.B;
		A += color.A;

		return *this;
	}

	vColor& vColor::operator-=(const vColor& color) {
		R -= color.R;
		G -= color.G;
		B -= color.B;
		A -= color.A;

		return *this;
	}

	vColor vColor::operator-() const {
		return vColor(-R, -G, -B);
	}

	vColor& vColor::operator*=(const vColor& color) {
		R *= color.R;
		G *= color.G;
		B *= color.B;
		A *= color.A;

		return *this;
	}

	vColor& vColor::operator*=(const vReal fFactor) {
		R *= fFactor;
		G *= fFactor;
		B *= fFactor;
		A *= fFactor;

		return *this;
	}

	vColor& vColor::operator=(const vColor& color) {
		if (&color != this) {
			R = color.R;
			G = color.G;
			B = color.B;
			A = color.A;
		}
		return *this;
	}

	vBool vColor::operator==(const vColor& colorB) const {
		return (R == colorB.R) && (G == colorB.G)
				&& (B == colorB.B) && (A == colorB.A);
	}

	vColor vColor::operator*(const vReal fFactor) {
		return vColor(R * fFactor, G * fFactor, B * fFactor);
	}

	vColor vColor::operator*(const vColor& colorB) {
		return vColor(R * colorB.R, G * colorB.G, B * colorB.B); 
	}

	vColor vColor::operator+(const vColor& colorB) {
		return vColor(R + colorB.R, G + colorB.G, B + colorB.B);
	}

	vColor vColor::operator-(const vColor& colorB) {
		return vColor(R - colorB.R, G - colorB.G, B - colorB.B);
	}

	/**************************************************************
	* Helpful functions                                           *
	***************************************************************/
	vVoid vColor::ClipComponents() {
		R = (R < 0.0f) ? 0.0f : (R > 1.0f ? 1.0f : R);
		G = (G < 0.0f) ? 0.0f : (G > 1.0f ? 1.0f : G);
		B = (B < 0.0f) ? 0.0f : (B > 1.0f ? 1.0f : B);
	}

	vVoid vColor::PositiveClipComponents() {
		R = (R > 1.0f) ? 1.0f : R;
		G = (G > 1.0f) ? 1.0f : G;
		B = (B > 1.0f) ? 1.0f : B;
		A = (A > 1.0f) ? 1.0f : A;
	}

	vWinColor vColor::ToWinColor(const vColor& color) {
		return RGB(color.R * 255.0f, 
			color.G * 255.0f, 
			color.B * 255.0f);
	}

	vColor vColor::FromWinColor(const vWinColor& color) {
		return vColor((vReal) GetRValue(color) / 255, 
			(vReal) GetGValue(color) / 255, 
			(vReal) GetBValue(color) / 255);
	}

	vColor vColor::AlphaBlend(vReal alpha, vColor &c1, vColor &c2) {
		return c1 * ( 1.0f - alpha ) + c2 * alpha;
	}
}